//
//  Interfaces.cpp
//  class
//
//  Created by Threese on 2019/7/23.
//  Copyright © 2019 Threese. All rights reserved.
//

#include "Interfaces.h"
#include <fstream>

namespace Terminal {
    //class Date
    Terminal:: Date:: Date(UINT year, UINT month, UINT day,
                           UINT hour, UINT minute, UINT second){
        m_iYear = year;
        m_iMonth = month;
        m_iDay = day;
        m_iHour = hour;
        m_iMinute = minute;
        m_iSecond = second;
    }
    
    void Terminal:: Date:: changeTo(UINT year, UINT month, UINT day,
                                    UINT hour, UINT minute, UINT second){
        m_iYear = year;
        m_iMonth = month;
        m_iDay = day;
        m_iHour = hour;
        m_iMinute = minute;
        m_iSecond = second;
    }
    
    
    //Input
    void Terminal:: Date:: setYear(UINT year){
        m_iYear = year;
    }
    
    void Terminal:: Date:: setMonth(UINT month){
        m_iMonth = month;
    }
    
    void Terminal:: Date:: setDay(UINT day){
        m_iDay = day;
    }
    
    void Terminal:: Date:: setHour(UINT hour){
        m_iHour = hour;
    }
    
    void Terminal:: Date:: setMinute(UINT minute){
        m_iMinute = minute;
    }
    
    void Terminal:: Date:: setSecond(UINT second){
        m_iSecond = second;
    }
    
    
    //Output
    UINT Terminal:: Date:: year(){
        return m_iYear;
    }
    
    UINT Terminal:: Date:: month(){
        return m_iMonth;
    }
    
    UINT Terminal:: Date:: day(){
        return m_iDay;
    }
    
    UINT Terminal:: Date:: hour(){
        return m_iHour;
    }
    
    UINT Terminal:: Date:: minute(){
        return m_iMinute;
    }
    
    UINT Terminal:: Date:: second(){
        return m_iSecond;
    }
    
    const char * Terminal:: Date:: toString(){
        std::string *temp = new std::string("");
        
        *temp += std::to_string(this->year());
        *temp += "/";
        *temp += std::to_string(this->month());
        *temp += "/";
        *temp += std::to_string(this->day());
        *temp += "\t";
        *temp += std::to_string(this->hour());
        *temp += ":";
        *temp += std::to_string(this->minute());
        *temp += ":";
        *temp += std::to_string(this->second());

        return temp->data();
    }
    
    
    //Operators
    bool  Terminal:: Date:: operator==(Date& date){
        if(this->year() == date.year()     &&
           this->month() == date.month()   &&
           this->day() == date.day()       &&
           this->hour() == date.hour()     &&
           this->minute() == date.minute() &&
           this->second() == date.second()){
            return true;
        }
        
        return false;
    }
    
    UINT Terminal:: Date:: operator- (Date& date){
        int dayInMonth[] = {31, 28, 31, 30, 31, 30,
            31, 31, 30, 31, 30, 31};
        
        int result = 0;
        
        int year = this->year() - date.year();
        
        int mDay1 = 0;
        for(int i = 0;i < this->month();i++)
            mDay1 += dayInMonth[i];
        
        int mDay2 = 0;
        for(int i = 0;i < date.month();i++)
            mDay2 += dayInMonth[i];
        
        int day = this->day() - date.day();
        
        result = year * 365 + (mDay1 - mDay2) + day;
        
        if(result >= 0)
            return result;
        else
            return -result;
    }
    
    void  Terminal:: Date:: operator= (Date& date){
        this->changeTo(date.year(), date.month(), date.day(),
                       date.hour(), date.minute(), date.second());
    }
    
    bool  Terminal:: Date:: operator> (Date& date){
        if(this->year() > date.year())
            return true;
        else if(this->year() == date.year()){
            if(this->month() > date.month())
                return true;
            else if(this->month() == date.month()){
                if(this->day() > date.day())
                    return true;
                else if(this->day() == date.day()){
                    if(this->hour() > date.hour())
                        return true;
                    else if(this->hour() == date.hour()){
                        if(this->minute() > date.minute())
                            return true;
                        else if(this->minute() == date.minute() ){
                            if(this->second() > date.second())
                                return true;
                            else if(this->second() == date.second())
                                return false;
                            else
                                return false;
                        }
                        else
                            return false;
                    }
                    else
                        return false;
                }
                else
                    return false;
            }
            else
                return false;
        }
        else
            return false;
    }
    
    bool  Terminal:: Date:: operator< (Date& date){
        if(this->year() > date.year())
            return false;
        else if(this->year() == date.year()){
            if(this->month() > date.month())
                return false;
            else if(this->month() == date.month()){
                if(this->day() > date.day())
                    return false;
                else if(this->day() == date.day()){
                    if(this->hour() > date.hour())
                        return false;
                    else if(this->hour() == date.hour()){
                        if(this->minute() > date.minute())
                            return false;
                        else if(this->minute() == date.minute() ){
                            if(this->second() > date.second())
                                return false;
                            else if(this->second() == date.second())
                                return false;
                            else
                                return true;
                        }
                        else
                            return true;
                    }
                    else
                        return true;
                }
                else
                    return true;
            }
            else
                return true;
        }
        else
            return true;
    }
    
    std:: ostream &operator << (std::ostream &os, Date &date){
        os << date.year() << "/" << date.month() << "/" << date.day()
        << "\t" << date.hour() << ":" << date.minute() << ":" << date.second();
        
        return os;
    }
    
    
    
    
    //class Task
    Terminal:: Task:: Task(std::string name, std::string label, std::string remarks,
                           Date* pBegDate, Date* pEndDate, int imp){
        m_strName = name;
        m_strLabel = label;
        m_strRemarks = remarks;
        m_pBegDate = new Date(0, 0, 0, 0, 0, 0);
        *m_pBegDate = *pBegDate;
        m_pEndDate = new Date(0, 0, 0, 0, 0, 0);
        *m_pEndDate = *pEndDate;
        m_iImportance = imp;
        m_pNextTask = nullptr;
        m_pPreTask = nullptr;
    }
    
    Terminal:: Task:: ~Task(){
        if(m_pBegDate){
            delete m_pBegDate;
            m_pBegDate = nullptr;
        }
        
        if(m_pEndDate){
            delete m_pEndDate;
            m_pEndDate = nullptr;
        }
        
        if(m_pNextTask){
            delete m_pNextTask;
            m_pNextTask = nullptr;
        }

        if(m_pPreTask){
            delete m_pPreTask;
            m_pPreTask = nullptr;
        }
    }
    
    
    //Input
    void Terminal:: Task:: add(Task* newTask){
        Task *ptr = this;
        while (ptr->next()) {
            ptr = ptr->next();
        }

        ptr->setNext(newTask);

        newTask->setPre(ptr);
        newTask->setNext(nullptr);
        
        
    }
    
    void Terminal:: Task:: add(std::string name, std::string label, std::string remarks,
                                Date* pBegDate, Date* pEndDate, int imp){
        Task *temp = new Task(name, label, remarks,
                          pBegDate, pEndDate, imp);
        
        this->add(temp);
    }
    
    Task* Terminal:: Task:: remove(std::string name){
        Task *target = get(name);
        if(target){
            Task *pre = target->pre();
            Task *next = target->next();
            
            if(pre)
                pre->setNext(next);

            if(next)
                next->setPre(pre);
            
            target->setNext(nullptr);
            target->setPre(nullptr);
            
           
            
            if(target == this){
                 delete target;
                 return next;
            }
            else{
                delete target;
                return this;
            }
        }
        
        return this;
    }
    
    void Terminal:: Task:: setName(std::string name){
        m_strName = name;
    }
    
    void Terminal:: Task:: setLabels(std::string labels){
        m_strLabel = labels;
    }
    
    void Terminal:: Task:: setRemarks(std::string remarks){
        m_strRemarks = remarks;
    }
    
    void Terminal:: Task:: setBegDate(Date* date){
        *m_pBegDate = *date;
    }
    
    void Terminal:: Task:: setEndDate(Date* date){
        *m_pEndDate = *date;
    }
    
    void Terminal:: Task:: setImportance(int imp){
        m_iImportance = imp;
    }

    
    //Output
    Task* Terminal:: Task:: at(int index){
        Task *ptr = this;
        
        for(int i = 0;ptr;++i){
            if(index == i)
                return ptr;
            
            ptr = ptr->next();
        }
        
        return nullptr;
    }
    
    Task* Terminal:: Task:: get(std::string name){
        Task *ptr = this;
        
        while (ptr) {
            if(ptr->name() == name)
                return ptr;
            
            ptr = ptr->next();
        }
        
        return nullptr;
    }
    
    Task* Terminal:: Task:: next(){
        return m_pNextTask;
    }
    
    Task* Terminal:: Task:: pre(){
        return m_pPreTask;
    }
    
    const char * Terminal:: Task:: name(){
        return m_strName.data();
    }
    
    const char * Terminal:: Task:: label(){
        return m_strLabel.data();
    }
    
    const char * Terminal:: Task:: remarks(){
        return m_strRemarks.data();
    }
    
    Date*       Terminal:: Task:: begDate(){
        return m_pBegDate;
    }
    
    Date*       Terminal:: Task:: endDate(){
        return m_pEndDate;
    }
    
    int         Terminal:: Task:: Importance(){
        return m_iImportance;
    }
    
    const char * Terminal:: Task:: toString(){
        std::string *temp = new std::string("");
        
        *temp += "name: ";
        *temp += this->name();
        *temp += "\n";
        *temp += "label: ";
        *temp += this->label();
        *temp += "\n";
        *temp += "remarks: ";
        *temp += this->remarks();
        *temp += "\n";
        *temp += "beginDate: ";
        *temp += this->begDate()->toString();
        *temp += "\n";
        *temp += "endDate: ";
        *temp += this->endDate()->toString();
        *temp += "\n";
        *temp += "improtance: ";
        *temp += std::to_string(this->Importance());
        
        return temp->c_str();
    }
    
    //New methods
    void Terminal:: Task:: swapWith(Task *t){
        if(t){
            if(this->next() == t){
                Task *pre = this->pre();
                Task *next = t->next();
                
                if(pre)
                    pre->setNext(t);
                if(next)
                    next->setPre(this);
                
                this->setPre(t);
                t->setNext(this);
                
                this->setNext(next);
                t->setPre(pre);
            }
            else if(this->pre() == t){
                Task *next = this->next();
                Task *pre = t->pre();
                
                if(next)
                    next->setPre(t);
                if(pre)
                    pre->setNext(this);
                
                this->setPre(pre);
                this->setNext(t);
                t->setPre(this);
                t->setNext(next);
            }
            else{
                Task *pre1 = this->pre();
                Task *pre2 = t->pre();
                Task *next1 = this->next();
                Task *next2 = t->pre();
                
                this->setPre(pre2);
                this->setNext(next2);
                t->setPre(pre1);
                t->setNext(next1);
                
                if(pre1)
                    pre1->setNext(t);
                if(next1)
                    next1->setPre(t);
                if(pre2)
                    pre2->setNext(this);
                if(next2)
                    next2->setNext(this);
            }
        }
    }
    
    std:: ostream &operator << (std::ostream &os, Task &task){
        Task *ptr = &task;
        for(int i = 1;ptr;i++) {
            os << "task " << i << std::endl;
            os << "name: "    << ptr->name() << std::endl;
            os << "label: "   << ptr->label() << std::endl;
            os << "remarks: " << ptr->remarks() << std::endl;
            os << "beginDate: " << ptr->begDate()->toString() << std::endl;
            os << "endDate: " << ptr->endDate()->toString() << std::endl;
            os<< "importance: " << ptr->Importance() << std::endl;
            os << std::endl << std::endl;
            
            ptr = ptr->next();
        }
        
        return os;
    }
    
    
    
    
    //protected method
    void Terminal:: Task:: setPre(Task* t){
        this->m_pPreTask = t;
    }
    
    Task* Terminal:: Task:: getPre(){
        return m_pPreTask;
    }
    
    void Terminal:: Task:: setNext(Task* t){
        this->m_pNextTask = t;
    }
    
    Task* Terminal:: Task:: getNext(){
        return m_pNextTask;
    }
    
    
    
    
    //class Plan
    Terminal:: Plan:: Plan(std::string name, std::string label, std::string remarks){
        m_strName = name;
        m_strLabel = label;
        m_strRemarks = remarks;
        
        m_pPrePlan = nullptr;
        m_pNextPlan = nullptr;
        m_pFirstTask = nullptr;
    }
    
    Terminal:: Plan:: ~Plan(){
        if(m_pPrePlan){
            delete m_pPrePlan;
            m_pPrePlan = nullptr;
        }
        
        if(m_pNextPlan){
            delete m_pNextPlan;
            m_pNextPlan = nullptr;
        }
        
        if(m_pFirstTask){
            delete m_pFirstTask;
            m_pFirstTask = nullptr;
        }
    }
    
    
    // Input
    void Terminal:: Plan:: add(Plan* newPlan){
        Plan *ptr = this;
        while (ptr->next()) {
            ptr = ptr->next();
        }
        
        newPlan->setPre(ptr);
        newPlan->setNext(nullptr);
        
        ptr->setNext(newPlan);
    }
    
    void Terminal:: Plan:: add(std::string name, std::string label, std::string remarks){
        Plan *p = new Plan(name, label, remarks);
        
        add(p);
    }
    
   Plan* Terminal:: Plan:: remove(std::string name){
        Plan *target = get(name);
        if(target){
            Plan *pre = target->pre();
            Plan *next = target->next();
            
            if(pre)
                pre->setNext(next);
            
            if(next)
                next->setPre(pre);

            target->setPre(nullptr);
            target->setNext(nullptr);
            
            if(target == this){
                delete target;
                return next;
            }
            else{
                delete target;
                return this;
            }
        }
        
        return this;
    }
    
    void Terminal:: Plan:: setName(std::string name){
        m_strName = name;
    }
    
    void Terminal:: Plan:: setLabel(std::string label){
        m_strLabel = label;
    }
    
    void Terminal:: Plan:: setRemarks(std::string remark){
        m_strRemarks = remark;
    }
    
    
    // Output
    Plan* Terminal:: Plan:: at(int index){
        Plan *ptr = this;
        for(int i = 0;ptr;++i){
            if(index == i)
                return ptr;
            
            ptr = ptr->next();
        }
        
        return nullptr;
    }
    
    Plan* Terminal:: Plan:: get(std::string name){
        Plan *ptr = this;
        while(ptr){
            if(ptr->name() == name)
                return ptr;
            
            ptr = ptr->next();
        }
        
        return nullptr;
    }
    
    Plan* Terminal:: Plan:: next(){
        return m_pNextPlan;
    }
    
    Plan* Terminal:: Plan:: pre(){
        return m_pPrePlan;
    }
    
    const char* Terminal:: Plan:: name(){
        return m_strName.data();
    }
    
    const char* Terminal:: Plan:: label(){
        return m_strLabel.c_str();
    }
    
    const char* Terminal:: Plan:: remarks(){
        return m_strRemarks.data();
    }
    
    Task* Terminal:: Plan:: getFirstTask(){
        return m_pFirstTask;
    }
    
    const char * Terminal:: Plan:: toString(){
        std::string *temp = new std::string("");
        
        *temp += "name: ";
        *temp += this->name();
        *temp += "\nlabel: ";
        *temp += this->label();
        *temp += "\nremarks: ";
        *temp += this->remarks();
        *temp += "\nstartDate: ";
        *temp += this->getBegDate()->toString();
        *temp += "\n";
        *temp += "endDate: ";
        *temp += this->getEndDate()->toString();
        *temp += "\n\n\n";
        
        Task *t = this->getFirstTask();
        for(int i = 0; t ;++i){
            *temp += "task ";
            *temp += i + 1;
            *temp += "\n";
            *temp += t->toString();
            *temp += "\n";
            
            t = t->next();
        }
        
        return temp->data();
    }
    
    
    //New methods
    void Terminal:: Plan:: addTask(Task *t){
        if(m_pFirstTask)
            m_pFirstTask->add(t);
        else
            m_pFirstTask = t;
    }
    
   void Terminal:: Plan:: removeTask(std::string name){
        m_pFirstTask = m_pFirstTask->remove(name);
    }
    
    Task* Terminal:: Plan:: findTask(std::string name){
        return m_pFirstTask->get(name);
    }
    
    Date* Terminal:: Plan:: getBegDate(){
        if(m_pFirstTask){
            Date* begDate = m_pFirstTask->begDate();
            
            Task * ptr = m_pFirstTask->next();
            while(ptr){
                if(ptr->begDate() < begDate)
                    begDate = ptr->begDate();
                
                ptr = ptr->next();
            }
            return begDate;
        }
        return nullptr;
    }
    
    Date* Terminal:: Plan:: getEndDate(){
        if(m_pFirstTask){
            Date* endDate = m_pFirstTask->endDate();
            
            Task * ptr = m_pFirstTask->next();
            while(ptr){
                if(ptr->endDate() > endDate)
                    endDate = ptr->endDate();
                
                ptr = ptr->next();
            }
            return endDate;
        }
        return nullptr;
    }
    
    void Terminal:: Plan:: swapWith(Plan *p){
       if(p){
            if(this->next() == p){
                Plan *pre = this->pre();
                Plan *next = p->next();
                
                if(pre)
                    pre->setNext(p);
                if(next)
                    next->setPre(this);
                
                this->setPre(p);
                p->setNext(this);
                
                this->setNext(next);
                p->setPre(pre);
            }
            else if(this->pre() == p){
                Plan *next = this->next();
                Plan *pre = p->pre();
                
                if(next)
                    next->setPre(p);
                if(pre)
                    pre->setNext(this);
                
                this->setPre(pre);
                this->setNext(p);
                p->setPre(this);
                p->setNext(next);
            }
            else{
                Plan *pre1 = this->pre();
                Plan *pre2 = p->pre();
                Plan *next1 = this->next();
                Plan *next2 = p->pre();
                
                this->setPre(pre2);
                this->setNext(next2);
                p->setPre(pre1);
                p->setNext(next1);
                
                if(pre1)
                    pre1->setNext(p);
                if(next1)
                    next1->setPre(p);
                if(pre2)
                    pre2->setNext(this);
                if(next2)
                    next2->setNext(this);
            }
        }
    }
    
    std:: ostream &operator << (std::ostream &os, Plan &plan){
        Plan *ptr = &plan;
        for(int i = 0;ptr;i++){
            os << "plan " << i+1 << std::endl;
            os << "name: " << ptr->name() << std::endl;
            os << "label: " << ptr->label() << std::endl;
            os << "remarks: " << ptr->remarks() << std::endl;
            os << "beginDate" << ptr->getBegDate()->toString() << std::endl;
            os << "endDate" << ptr->getEndDate()->toString() << std::endl;
            os << std::endl;
            os << *ptr->m_pFirstTask;
            os << std::endl << std::endl;
            
            ptr = ptr->next();
        }
        
        return os;
    }
    
    
    
    
    //protected
    Plan* Terminal:: Plan:: getPre(){
        return m_pPrePlan;
    }
    
    void        Terminal:: Plan:: setPre(Plan *p){
        m_pPrePlan = p;
    }
    
    Plan* Terminal:: Plan:: getNext(){
        return m_pNextPlan;
    }
    
    void       Terminal:: Plan:: setNext(Plan *p){
        m_pNextPlan = p;
    }
    
    
    //Functiooooon
    //sort
    void STimeSort(Task *h){
        Task * temp = h;
        
        if(temp){
            int size = 0;
            while(temp->next()){
                temp->next();
                size++;
            }
            
            temp = h;
            for(int i = 0; i < size; ++i){
                for(int j = i; j < size; ++j){
                    if(temp->begDate() > temp->next()->begDate())
                        temp->swapWith(temp->next());
                    temp = temp->next();
                }
                temp = h;
            }
        }
    }
    
    void ETimeSort(Task *h){
        Task * temp = h;
        
        if (temp) {
            int size = 0;
            while(temp->next()){
                temp->next();
                size++;
            }
            
            temp = h;
            for(int i = 0; i < size; ++i){
                for(int j = i; j < size; ++j){
                    if(temp->endDate() > temp->next()->endDate())
                        temp->swapWith(temp->next());
                    temp = temp->next();
                }
                temp = h;
            }
        }
    }
    
    void ImpSort(Task *h){
        Task * temp = h;
        
        if(temp){
            int size = 0;
            while(temp->next()){
                temp->next();
                size++;
            }
            
            temp = h;
            for(int i = 0; i < size; ++i){
                for(int j = i; j < size; ++j){
                    if(temp->Importance() < temp->next()->Importance())
                        temp->swapWith(temp->next());
                    temp = temp->next();
                }
                temp = h;
            }
        }
    }
    
    void STimeSort(Plan *h){
        Plan * temp = h;

        if (temp) {
            int size = 0;
            while(temp->next()){
                temp->next();
                size++;
            }
            
            temp = h;
            for(int i = 0; i < size; ++i){
                for(int j = i; j < size; ++j){
                    if(temp->getBegDate() > temp->next()->getBegDate())
                        temp->swapWith(temp->next());
                    temp = temp->next();
                }
                temp = h;
            }
        }
    }
    
    void ETimeSort(Plan *h){
        Plan * temp = h;
        
        if(temp){
            int size = 0;
            while(temp->next()){
                temp->next();
                size++;
            }
            
            temp = h;
            for(int i = 0; i < size; ++i){
                for(int j = i; j < size; ++j){
                    if(temp->getEndDate() > temp->next()->getEndDate())
                        temp->swapWith(temp->next());
                    temp = temp->next();
                }
                temp = h;
            }
        }
    }
    
    
    //size
    int GetLength(Plan *p){
        int count = 0;
        
        Plan *temp = p;
        
        while(temp){
            temp = temp->next();
            count++;
        }
        
        return count;
    }
    
    int GetLength(Task *p){
        int count = 0;
        
        Task *temp = p;
        
        while(temp){
            temp = temp->next();
            count++;
        }
        
        return count;
    }
    
    //I&O
    int checkHead(std::string buff){
        std::string head = "06hkllxx";
        
        if(buff == head)
            return 1;
        else
            return 0;
    }
    
    Plan *TOpen(std::string fileName){
        using namespace std;
        Plan *returnP = new Plan("","","");
        
        ifstream fin;
        fin.open(fileName);
        
        if(fin.is_open()){
            string buff;
            int numBuff;
            getline(fin, buff);
            
            if(checkHead(buff) == 1){
                
                int planSize = 0;
                fin >> planSize;
                getline(fin, buff);
                
                for(int i = 0;i < planSize;i++){
                    Plan *p = new Plan("","","");
                    getline(fin, buff);
                    p->setName(buff);
                    getline(fin, buff);
                    p->setLabel(buff);
                    
                    //remarks block
                    getline(fin, buff);
                    
                    string rm = "";
                    while(buff != "$#$remarrrrrrksE"){
                        rm += buff;
                        getline(fin, buff);
                        if(buff == "$#$remarrrrrrksE")
                            break;
                        else
                            rm += "\n";
                    }
                    p->setRemarks(rm);
                    
                    int taskSize = 0;
                    fin >> taskSize;
                    getline(fin, buff);
                    //debug
                    
                    for (int j = 0; j < taskSize; j++) {
                        
                        Date * sDate1 = new Date(0, 0, 0, 0, 0 ,0);
                        Date * eDate1 = new Date(0, 0, 0, 0, 0, 0);
                        
                        
                        
                        Task *taskPtr = new Task("","","",sDate1,eDate1);
                        getline(fin, buff);
                        taskPtr->setName(buff);
                        getline(fin, buff);
                        taskPtr->setLabels(buff);
                        
                        //remarks block
                        getline(fin, buff);
                        rm = "";
                        while(buff != "$#$remarrrrrrksE"){
                            rm += buff;
                            getline(fin, buff);
                            
                            if(buff == "$#$remarrrrrrksE")
                                break;
                            else
                                rm += "\n";
                        }
                        taskPtr->setRemarks(rm);
                        
                        int year, month, day, hour, minute, second;
                        fin >> year >> month >> day >> hour >> minute >> second;
                        getline(fin, buff);
                        Date *bDate = new Date(year, month, day,
                                               hour, minute, second);
                        taskPtr->setBegDate(bDate);
                        fin >> year >> month >> day >> hour >> minute >> second;
                        getline(fin, buff);
                        bDate->changeTo(year, month, day, hour, minute, second);
                        taskPtr->setEndDate(bDate);
                        fin >> numBuff;
                        getline(fin, buff);
                        taskPtr->setImportance(numBuff);
                        
                        p->addTask(taskPtr);
                    }
                    
                    returnP->add(p);
                }
                fin.close();
                
                return returnP->next();
            }
        }
        
        return nullptr;
    }
    
    void TSave(std::string fileName, Plan *pp){
        using namespace std;
        ofstream fout;
        fout.open(fileName);
        
        if(fout.is_open()){
            fout << "06hkllxx" << endl;
            
            int planSize = GetLength(pp);
            fout << planSize << endl;
            
            Plan *p = pp;
            for(int i = 0;i < planSize;i++){
                fout << p->name() << endl;
                fout << p->label() << endl;
                fout << p->remarks() << endl;
                fout << "$#$remarrrrrrksE" << endl;
                
               
                int taskSize = GetLength(p->getFirstTask());
                fout << taskSize << endl;
                Task *t = p->getFirstTask();
                for(int j = 0;j < taskSize;j++){
                    fout << t->name() << endl;
                    fout << t->label() << endl;
                    fout << t->remarks() << endl;
                    fout << "$#$remarrrrrrksE" << endl;
                    Date *bD = t->begDate();
                    fout << bD->year() << " " << bD->month() << " " <<
                    bD->day() << " " << bD->hour() << " " << bD->minute() <<
                    " " << bD->second() << endl;
                    Date *eD = t->endDate();
                    fout << eD->year() << " " << eD->month() << " " <<
                    eD->day() << " " << eD->hour() << " " << eD->minute() <<
                    " " << eD->second() << endl;
                    fout << t->Importance() << endl;
                        
                        t = t->next();
                    }
                
                p = p->next();
                }
            
            fout.close();
        }
        else{
            //TODO
        }
    }
    
    Task* checkToday(Plan *p, Date *today){
        Date * sDate1 = new Date(0, 0, 0, 0, 0 ,0);
        Date * eDate1 = new Date(0, 0, 0, 0, 0, 0);   
        Task *t = new Task("", "", "", sDate1, eDate1);
        
        Plan *pPtr = p;
        while (pPtr) {
            std::cout << pPtr->name() << std::endl;
            Task *tPtr = pPtr->getFirstTask();
            while (tPtr) {
                if(*today < *tPtr->endDate()
                   && *today > *tPtr->begDate()){
                    Date * sDate = new Date(0, 0, 0, 0, 0 ,0);
                    Date * eDate = new Date(0, 0, 0, 0, 0, 0);   
                    Task *tTemp = new Task("","","",sDate,eDate);
                    tTemp->setName(tPtr->name());
                    tTemp->setLabels(tPtr->label());
                    tTemp->setRemarks(tPtr->remarks());
                    tTemp->setBegDate(tPtr->begDate());
                    tTemp->setEndDate(tPtr->endDate());
                    tTemp->setImportance(tPtr->Importance());
                    
                    t->add(tTemp);
                }
                
                tPtr = tPtr->next();
            }
            
            pPtr = pPtr->next();
        }
        
        return t->next();
    }
}
